package com.lwj.liwanjia.view.activity.logistics;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.RecordAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.KdBean;
import com.lwj.liwanjia.impl.KdView;
import com.lwj.liwanjia.presenter.KdPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 快递记录
 */
public class KuaidiRecordActivity extends BaseActivity implements KdView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rlv_record)
    SuperRecyclerView rlvRecord;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private RecordAdapter adapter;
    private List<KdBean.DataBean> list = new ArrayList<>();
    private String token;
    private KdPresenter kdPresenter;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("快递记录");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvRecord.setLayoutManager(manager);
        rlvRecord.setRefreshEnabled(false);
        rlvRecord.setLoadMoreEnabled(false);
        adapter = new RecordAdapter(this, list);
        rlvRecord.setAdapter(adapter);
        token = SpfUtils.getSpfUtils(this).getToken();
        kdPresenter = new KdPresenter(this);

    }

    @Override
    protected void initData() {
        kdPresenter.getKdData(token);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_kuaidi_record;
    }


    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getKdData(KdBean kdBean) {
        if (kdBean.getState() == 0) {
            Toast.makeText(this, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (kdBean.getState() == 1) {
            list.clear();
            list.addAll(kdBean.getData());
            adapter.notifyDataSetChanged();
        }
    }
}
